package io.renren.modules.com.controller;

import java.util.Arrays;
import java.util.List;

import io.renren.modules.com.entity.vo.DictBatchVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.com.entity.DictEntity;
import io.renren.modules.com.service.DictService;
import io.renren.common.utils.Pages;
import io.renren.common.utils.ResponseModel;


/**
 * 业务-字典
 *
 * @author xuewenjun
 * @email xwj@kafukeji.cn
 * @date 2022-03-18 09:58:29
 */
@RestController
@RequestMapping("com/dict")
@Api(tags = "com-dict")
public class DictController {
    @Autowired
    private DictService dictService;

    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public ResponseModel<Pages<DictEntity>> list(@RequestBody DictEntity dict){
        Pages<DictEntity> page = dictService.queryPage(dict);
        return ResponseModel.of(page);
    }

    @PostMapping("/listAll")
    @ApiOperation("查询所有")
    public ResponseModel<List<DictEntity>> listAll(@RequestBody DictEntity dict) {
        List<DictEntity> list = dictService.listAll(dict);
        return ResponseModel.of(list);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("详情")
    public ResponseModel<DictEntity> info(@PathVariable("id") Long id){
        DictEntity dict = dictService.getById(id);

        return ResponseModel.of(dict);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("新增")
    public ResponseModel<DictEntity> save(@RequestBody DictEntity dict){
        dictService.save(dict);
        return ResponseModel.of(dict);
    }

    @PostMapping("/saveOrUpdateBatch")
    @ApiOperation("批量新增或保存")
    public ResponseModel saveOrUpdateBatch(@RequestBody List<DictEntity> lists) {
        dictService.saveOrUpdateBatch(lists);
        return ResponseModel.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseModel<DictEntity> update(@RequestBody DictEntity dict){
        dictService.updateById(dict);

        return ResponseModel.of(dict);
    }

    /**
     * 条件删除
     * @param dict
     * @return
     */
    @PostMapping("/deleteByCondition")
    @ApiOperation("条件删除")
    public ResponseModel deleteByCondition(@RequestBody  DictEntity dict) {
        dictService.deleteByCondition(dict);
        return ResponseModel.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除或批量删除")
    public ResponseModel delete(@RequestBody Long[] ids){
        dictService.removeByIds(Arrays.asList(ids));
        return ResponseModel.ok();
    }

}
